package com.zxl.grocery.modular.schedule.controller;


import cn.hutool.core.bean.BeanUtil;
import com.zxl.grocery.core.base.controller.BaseController;
import com.zxl.grocery.core.common.annotion.Permission;
import com.zxl.grocery.core.common.exception.BizExceptionEnum;
import com.zxl.grocery.core.log.LogObjectHolder;
import com.zxl.grocery.core.reqres.response.ResponseData;
import com.zxl.grocery.core.util.ToolUtil;
import com.zxl.grocery.kernel.model.exception.ServiceException;
import com.zxl.grocery.modular.schedule.entity.ScheduleJobEntity;
import com.zxl.grocery.modular.schedule.model.ScheduleJobDto;
import com.zxl.grocery.modular.schedule.service.ScheduleJobMgrService;
import com.zxl.grocery.modular.schedule.warpper.ScheduleJobMgrWarpper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 定时任务控制器
 *
 * @author zxl
 * @Date 2017-08-23 14:20:31
 */
@Controller
@RequestMapping("/scheduleJobMgr")
public class ScheduleJobMgrController extends BaseController {

    private static String PREFIX = "/schedule/";

    @Autowired
    private ScheduleJobMgrService scheduleJobMgrService;

    /**
     * 跳转到定时任务首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "schedule.html";
    }

    /**
     * 跳转到添加定时任务
     */
    @RequestMapping(value = "/schedule_add")
    public String scheduleJobMgrAdd() {
    	return PREFIX + "schedule_add.html";
    }

    /**
     * 跳转到修改定时任务
     */
    @RequestMapping("/schedule_edit")
    public String scheduleJobMgrUpdate(@RequestParam Integer jobId) {
        if (ToolUtil.isEmpty(jobId)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
//        ScheduleJobEntity schelduleJob = scheduleJobMgrService.selectById(jobId);
//         LogObjectHolder.me().set(schelduleJob);
        return PREFIX + "schedule_edit.html";
    }

    /**
     * 获取定时任务列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(value = "beanName", required = false) String beanName) {
        //查询列表数据
        List<Map<String, Object>> result = this.scheduleJobMgrService.getAllScheduleJobList(beanName);
        return super.warpObject(new ScheduleJobMgrWarpper(result));
    }

    /**
     * 新增定时任务
     */
    @RequestMapping(value = "/add")
    @ResponseBody
	@Permission
    public Object add(ScheduleJobEntity scheduleJob) {
        this.scheduleJobMgrService.saveJod(scheduleJob);
        return SUCCESS_TIP;
    }

    /**
     * 删除定时任务
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam(value = "ids[]") Long[] ids) {//request中发送json数据用post方式发送Content-type用application/json;charset=utf-8方式发送的话，直接用springMVC的@RequestBody标签接收后面跟实体对象就行了，spring会帮你自动拼装成对象，如果Content-type设置成application/x-www-form-urlencoded;charset=utf-8就不能用spring的东西了，只能以常规的方式获取json串了http://blog.csdn.net/mingtianhaiyouwo/article/details/51459764
        this.scheduleJobMgrService.deleteBatch(ids);
        return SUCCESS_TIP;
    }


    /**
     * 修改定时任务
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public ResponseData update(ScheduleJobDto scheduleJobDto) {
		ScheduleJobEntity jobEntity = new ScheduleJobEntity();
		BeanUtil.copyProperties(scheduleJobDto,jobEntity);
        this.scheduleJobMgrService.update(jobEntity);
        return SUCCESS_TIP;
    }

    /**
     * 定时任务详情
     */
    @RequestMapping(value = "/detail/{jobId}")
    @ResponseBody
    public ResponseData detail(@PathVariable("jobId") Long jobId) {
		if (ToolUtil.isEmpty(jobId)) {
			throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
		}

        ScheduleJobEntity schedule = this.scheduleJobMgrService.queryObject(jobId);
		LogObjectHolder.me().set(schedule);
		Map<String, Object> scheduleMap = BeanUtil.beanToMap(schedule);
        return ResponseData.success(scheduleMap);
    }

    /**
     * 立即执行任务
     */
    @RequestMapping("/run")
    @ResponseBody
    public Object run(@RequestParam(value = "jobIds[]") Long[] jobIds) {
        if (ToolUtil.isEmpty(jobIds)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        this.scheduleJobMgrService.run(jobIds);

        return SUCCESS_TIP;
    }

    /**
     * 暂停定时任务
     */
    @RequestMapping("/pause")
    @ResponseBody
    public Object pause(@RequestParam(value = "jobIds[]") Long[] jobIds) {
        if (ToolUtil.isEmpty(jobIds)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        this.scheduleJobMgrService.pause(jobIds);
        return SUCCESS_TIP;
    }

    /**
     * 恢复定时任务
     */
    @RequestMapping("/resume")
    @ResponseBody
    public Object resume(@RequestParam(value = "jobIds[]") Long[] jobIds) {
        if (ToolUtil.isEmpty(jobIds)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        this.scheduleJobMgrService.resume(jobIds);

        return SUCCESS_TIP;
    }
}
